package net.lgy.apitest.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import net.lgy.apitest.model.DO.ApiCategoryDO;
import net.lgy.apitest.mapper.ApiCategoryMapper;
import net.lgy.apitest.service.ApiCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * API分类Service实现类
 */
@Service
public class ApiCategoryServiceImpl implements ApiCategoryService {

    @Autowired
    private ApiCategoryMapper apiCategoryMapper;

    @Override
    public ApiCategoryDO getById(Long id) {
        return apiCategoryMapper.selectById(id);
    }

    @Override
    public boolean save(ApiCategoryDO categoryDO) {
        return apiCategoryMapper.insert(categoryDO) > 0;
    }

    @Override
    public boolean updateById(ApiCategoryDO categoryDO) {
        return apiCategoryMapper.updateById(categoryDO) > 0;
    }

    @Override
    public boolean removeById(Long id) {
        return apiCategoryMapper.deleteById(id) > 0;
    }

    @Override
    public List<ApiCategoryDO> getByAccountNo(Long accountNo) {
        LambdaQueryWrapper<ApiCategoryDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApiCategoryDO::getAccountNo, accountNo);
        // 移除不存在的字段引用，使用id排序
        queryWrapper.orderByAsc(ApiCategoryDO::getId);
        return apiCategoryMapper.selectList(queryWrapper);
    }

    @Override
    public List<ApiCategoryDO> getTopCategoriesByAccountNo(Long accountNo) {
        LambdaQueryWrapper<ApiCategoryDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApiCategoryDO::getAccountNo, accountNo);
        queryWrapper.eq(ApiCategoryDO::getParentId, 0);
        // 移除不存在的字段引用，使用id排序
        queryWrapper.orderByAsc(ApiCategoryDO::getId);
        return apiCategoryMapper.selectList(queryWrapper);
    }
}